const vm = new Vue({
    el: '#main',
    data: {
        view: {
            end: false,
        },
        data: {
            going: [],
            end: [],
        },
        pageNum: 1,
    },
    methods: {
        contactTheTenant,
        goPage,
        fixImgUrl,
        toggleEnd(b) {
            this.view.end = b;
        },
        async delete_order(id) {
            const res = await ajax({
                url: 'home/order/getOrderList',
                data: {
                    id: id,
                },
            });
            if (res.data.err = 0) {
                toast('操作成功');
            }
            else {
                toast('操作失败');
            }
        },
        async acceptOrder(id) {
            const close = fxj.dialog.loading({noBg: true});
            const res = await ajax({
                url: 'home/order/acceptOrder',
                data: {orderId: id},
            });
            close();
            toast(res.msg);
            if (res.err === 0) {
                reqOrderList(1);
            }
        },
        async refuseOrder(id) {
            const res = await ajax({
                url: 'home/order/acceptOrder',
                data: {orderId: id},
            });
            console.log(res);
        },
        async rejectIndent(id) {
            const res = await ajax({
                url: 'home/order/refuseOrder',
                data: {orderId: id},
            });
            if (!res.msg) {
                res.msg = '操作成功';
            }
            toast(res.msg);
            if (res.err === 0) {
                reqOrderList(1);
            }
        },
        checkIndentDetail,
    },
});

function loadMore() {
    isToBottom(100, (ok, el) => {
        if (!ok) return;
        vm.pageNum += 1;
        reqOrderList(vm.pageNum);
    });
}

function apiready() {
    if (fxj.getSearch().get('type') === 'open') {
        document.querySelector('.back').style.display = '';
    }
    fxj.fixLayout();
    reqOrderList(1);
    loadMore();
    setCustomRefreshHeaderInfo(() => {
        reqOrderList(1);
    });
}

async function reqOrderList(pageNum) {
    let close = null;
    if (pageNum !== 1) {
        close = fxj.dialog.loading();
    }
    const res = await ajax({
        url: 'home/order/getOrderList',
        data: {
            pageNum,
            role: 1,
        },
    });
    if (close) {
        close();
    }
    if (res.data.underWay) {
        if (vm.pageNum > 1) {
            vm.data.going.push(...res.data.underWay);
        }
        else {
            vm.data.going = res.data.underWay;
        }
    }
    if (res.data.over) {
        if (vm.pageNum > 1) {
            vm.data.end.push(...res.data.over);
        }
        else {
            vm.data.end = res.data.over;
        }
    }
}

function fixImgUrl(url) {
    return fxj.fixImgUrl(url);
}

function contactTheTenant(indent) {
    goPage('chat.html', {
        data: {
            landlordId: indent.tenant_id,
            type: '联系房客',
        },
    });
}

function checkIndentDetail(id) {
    goPage('indent_detail.html', {
        data: {
            indentId: id,
            role: 'fd',
        },
    });
}
